package com.wsd.smartcarwasher.controller;

import com.wsd.smartcarwasher.annotation.Log;
import com.wsd.smartcarwasher.constans.Global;
import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.Car;
import com.wsd.smartcarwasher.domain.UserInfo;
import com.wsd.smartcarwasher.service.CarService;
import com.wsd.smartcarwasher.service.WxService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "用户绑定的汽车信息相关接口")
@Controller
@CrossOrigin
public class CarController {

    @Resource
    private CarService carService;

    @Resource
    private WxService wxService;

    @ApiOperation(value="查询用户绑定的车辆信息", notes = "小程序车辆信息页", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "car", value = "车辆信息", required = true, dataType = "Car")
    @RequestMapping(value = "/v1/cars", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap queryCarByUserId(HttpServletRequest request) {
        String sessionId = request.getHeader(Global.AUTHORIZATION);
        if (StringUtils.isBlank(sessionId)) {
            return ReturnUtil.Error(ResultState.UNAUTHENTICATION_ERROR.getMsg());
        }
        
        UserInfo user = wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return ReturnUtil.Error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        return ReturnUtil.Success(ResultState.CAR_RECORD_QUERY_SUCCESS.getMsg(), carService.getCarsByUserId(user.getUserId()));
    }
    @Log("添加了车辆")
    @ApiOperation(value="添加绑定新车", notes = "小程序车辆信息页", produces = "application/json", response = ModelMap.class)
   // @ApiImplicitParam(name = "car", value = "车辆信息", required = true, dataType = "Car")
    @RequestMapping(value = "/v1/add_car", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap addCar(@RequestBody @ApiParam Car car, HttpServletRequest request) {
        String sessionId = request.getHeader(Global.AUTHORIZATION);
        if (StringUtils.isBlank(sessionId)) {
            return ReturnUtil.Error(ResultState.UNAUTHENTICATION_ERROR.getMsg());
        }
        UserInfo user = wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return ReturnUtil.Error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        if (car == null
                || StringUtils.isBlank(car.getPlateNumber())
                || StringUtils.isBlank(car.getCarModel())) {
            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
        }
        car.setUserId(user.getUserId());
        return carService.addCar(car);
    }
    @Log("移除了绑定信息")
    @ApiOperation(value="移除用户绑定的车辆", notes = "小程序车辆信息页", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "plateNumber", value = "车牌号", required = true, dataType = "String")
    @RequestMapping(value = "/v1/remove_car", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap removeCar(@RequestParam("plateNumber") String plateNumber,
                              HttpServletRequest request) {
        String sessionId = request.getHeader(Global.AUTHORIZATION);
        if (StringUtils.isBlank(sessionId)) {
            return ReturnUtil.Error(ResultState.UNAUTHENTICATION_ERROR.getMsg());
        }
        UserInfo user = wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return ReturnUtil.Error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        if (StringUtils.isBlank(plateNumber)) {
            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
        }
        return carService.removeCar(user.getUserId(), plateNumber);
    }

}
